package org.smth.search;

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

import org.smth.search.config.*;
import org.smth.search.config.impl.*;
import org.smth.search.fetcher.BoardFetcher;
import org.smth.search.fetcher.FetcherManager;
import org.smth.search.fetcher.FetcherThread;
import org.smth.search.fetcher.PageFetcher;
import org.smth.search.fetcher.impl.BoardFetcherImpl;
import org.smth.search.fetcher.impl.FetcherManagerImpl;
import org.smth.search.fetcher.impl.FetcherThreadImpl;
import org.smth.search.fetcher.impl.PageFetcherImpl;
import org.smth.search.indexer.IndexAnalyzer;
import org.smth.search.indexer.IndexManager;
import org.smth.search.indexer.Indexer;
import org.smth.search.indexer.impl.EnumAllAnalyzer;
import org.smth.search.indexer.impl.IndexManagerImpl;
import org.smth.search.indexer.impl.IndexerImpl;
import org.smth.search.parser.ContentParser;
import org.smth.search.parser.PostItemParser;
import org.smth.search.parser.PostParser;
import org.smth.search.parser.impl.ContentParserImpl;
import org.smth.search.parser.impl.PostItemParserImpl;
import org.smth.search.parser.impl.PostParserImpl;
import org.smth.search.persistence.DataService;
import org.smth.search.persistence.impl.DataServiceImpl;
import org.smth.search.query.QueryAnalyzer;
import org.smth.search.query.QueryParser;
import org.smth.search.query.QueryProvider;
import org.smth.search.query.impl.FieldQueryParser;
import org.smth.search.query.impl.QueryAnalyzerImpl;
import org.smth.search.query.impl.StaticQueryProviderImpl;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class SearcherModule extends AbstractModule {

	protected void configure() {
		bind(SiteConfig.class).to(HardCodeSiteConfig.class);
		bind(BoardConfig.class).to(HardCodeBoardConfig.class);
		bind(FetcherConfig.class).to(HardCodeFetcherConfig.class);
		bind(BoardFetcher.class).to(BoardFetcherImpl.class);
		bind(PageFetcher.class).to(PageFetcherImpl.class);
		bind(PostItemParser.class).to(PostItemParserImpl.class);
		bind(PostParser.class).to(PostParserImpl.class);
		bind(ContentParser.class).to(ContentParserImpl.class);
		bind(DataService.class).to(DataServiceImpl.class);
		bind(FetcherThread.class).to(FetcherThreadImpl.class);
		bind(FetcherManager.class).to(FetcherManagerImpl.class);
		bind(IndexerConfig.class).to(HardCodeIndexerConfig.class);
		bind(Indexer.class).to(IndexerImpl.class);
		bind(IndexAnalyzer.class).to(EnumAllAnalyzer.class);
		bind(IndexManager.class).to(IndexManagerImpl.class);
		bind(QueryConfig.class).to(HardCodeQueryConfig.class);
		bind(QueryProvider.class).to(StaticQueryProviderImpl.class);
		bind(QueryAnalyzer.class).to(QueryAnalyzerImpl.class);
		bind(QueryParser.class).to(FieldQueryParser.class);
		bind(String.class).annotatedWith(Names.named("DefaultField")).toInstance("post");

		final EntityManagerFactory factory = Persistence.createEntityManagerFactory("smth");
		bind(EntityManagerFactory.class).toInstance(factory);
		Runtime.getRuntime().addShutdownHook(new Thread() {
			public void run() {
				factory.close();
			}
		});
	}

}
